package com.grj.park.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.grj.park.common.R;
import com.grj.park.domain.User;
import com.grj.park.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@RestController
@RequiredArgsConstructor
@RequestMapping("/login")
public class LoginController {

    private final UserService userService;


    @PostMapping
    public R<?> login(@RequestBody User user){
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername,user.getUsername());
        User loginUser = userService.getOne(queryWrapper);
        if (Objects.isNull(loginUser)){
            R.fail("用户名或密码错误");
        }
        if (!Objects.equals(loginUser.getUsername(), user.getUsername()) || !Objects.equals(loginUser.getPassword(), user.getPassword())){
            R.fail("用户名或密码错误");
        }
        Map<String, Object> map = new HashMap<>();
        map.put("token",loginUser.getUsername());
        map.put("user",loginUser);
        return R.ok(map);
    }
}
